<?php	

//Product class
class Product
{
	public $id = 0;
	public $comment1;
	public $comment2;
	public $comment3;
	public $height;
	public $weight;
	public function IsElox(){
		if ( stristr($this->comment3, "elox" )!=false && $this->height>0 && $this->weight>0 ) {
			return true;
		}
		else
		{
			return false;
		}
	}
	public function IsKVS()	{
		if ( stristr($this->comment3, "kvs" )!=false && $this->height>0 && $this->weight>0 ) {
			return true;
		}
		else
		{
			return false;
		}
	}
	public function IsPaint(){
		if ( stristr($this->comment3, "kennz" )!=false ) {
			return true;
		}
		else
		{
			return false;
		}
	}
	public function GetBasePrice($string)
	{
		$query = "SELECT price FROM works WHERE name = '".$string."'";
		require_once("./includes/class.FetchFromMySQL.inc.php");
		$sql = new FetchFromMySQL("localhost", "szofttech", "alma");
		$sql->select_db("szofttech");
		$result = $sql->query($query);
		$result = $sql->FetchAsObject();
		if ( $sql->AffectedRows() != 0 ) return $result->price;
		return false;
	}
	public function GetEloxBasePrice() { //TODO
		return $this->GetBasePrice("elox");
	}
	public function GetKVSBasePrice() { //TODO		
		return $this->GetBasePrice("kvs");
	}
	public function GetPaintBasePrice() { //TODO		
		return $this->GetBasePrice("paint");
	}
	public function GetEloxPrice() {
		return ($this->height/1000)*($this->weight/1000)*$this->GetEloxBasePrice();
	}
	public function GetKVSPrice() {
		return ($this->height/1000)*($this->weight/1000)*$this->GetKVSBasePrice();
	}
	public function GetPaintPrice(){
		return $this->GetPaintBasePrice();
	}
	public function GetStandardPrice() { //TODO
		return $this->GetBasePrice("standard");
	}
	public function GetInvalidParts()
	{
		$PARTS = array();
		$query = "SELECT part.id as id , part.price as price , part.correctionmultiplier as correctionmultiplier , parts.quantity as quantity "
        . " FROM product , part , parts "
        . " WHERE product.id = parts.productid AND parts.partid = part.id "
        . " AND product.id = '".$this->id."'";
		
		require_once("./includes/class.Part.inc.php");
		require_once("./includes/class.FetchFromMySQL.inc.php");
		$sql = new FetchFromMySQL("localhost", "szofttech", "alma");
		//$sql->ConnectionTest();
		$sql->select_db("szofttech");
		$result = $sql->query($query);
		while ( $part = $sql->FetchAsObject('Part') ){
			// echo "id: {$part->id}<br>";
			// echo "price: {$part->price}<br>";
			// echo "correctionmultiplier: {$part->correctionmultiplier}<br>";
			// echo "quantity: {$part->quantity}<br>";
			// echo "GetPrice: {$part->GetPrice()}<br>";
			if ( !$part->IsValid() ) array_push($PARTS,$part);
		}
		return $PARTS;
	}
	public function GetPrice()	{
		$price = 0;
		//works
		$price += $this->GetEloxPrice();
		$price += $this->GetKVSPrice();
		$price += $this->GetPaintPrice();
		
		$price += $this->GetStandardPrice();
		
		//parts
		$query = "SELECT part.id as id , part.price as price , part.correctionmultiplier as correctionmultiplier , parts.quantity as quantity "
        . " FROM product , part , parts "
        . " WHERE product.id = parts.productid AND parts.partid = part.id "
        . " AND product.id = '".$this->id."'";
		
		require_once("./includes/class.Part.inc.php");
		require_once("./includes/class.FetchFromMySQL.inc.php");
		$sql = new FetchFromMySQL("localhost", "szofttech", "alma");
		//$sql->ConnectionTest();
		$sql->select_db("szofttech");
		$result = $sql->query($query);
		while ( $part = $sql->FetchAsObject('Part') ){
			// echo "id: {$part->id}<br>";
			// echo "price: {$part->price}<br>";
			// echo "correctionmultiplier: {$part->correctionmultiplier}<br>";
			// echo "quantity: {$part->quantity}<br>";
			// echo "GetPrice: {$part->GetPrice()}<br>";
			$price += $part->GetPrice();
		}
		return $price;
	}
};
?>